<template>
  <div class="project-box">
    <div class="title">
      <span @click="editProject(projectInfo.id)">{{ projectInfo.name }}</span>
    </div>
    <div class="createTime">
      <div class="time">
        <span>创建时间: {{formatTime(projectInfo.createAt)}}</span>
        <span>更新时间: {{formatTime(projectInfo.updateAt)}}</span>
      </div>
      <div class="operation">
        <span class="editor" @click="editProject(projectInfo.id)">编辑</span>
        <span class="remove" @click="removeProject(projectInfo.id)">删除</span>
      </div>
    </div>

    <!-- 弹框 -->
    <Dialog title="更新项目" :dialogVisible="dialogShow" :project="projectInfo" @close="handleClose" @update="handleUpdate"></Dialog>
  </div>
</template>

<script setup>
import { ref } from "vue"
import { formatTime } from "../../../utils/formatTime"
import { deleteProject } from "../../../service/api_project"
import { ElMessage } from "element-plus"
import Dialog from "../../../components/dialog/index.vue"
const props = defineProps({
  projectInfo: {
    type: Object,
    default: {}
  }
})
const emit = defineEmits(["update"])
// 控制弹框的显示与隐藏
const dialogShow = ref(false)
// 删除按钮
const removeProject = async (id) => {
  const {data} = await deleteProject(id)
  ElMessage.success(data.msg)
  emit("update")
}
// 编辑按钮
const editProject = () => {
  dialogShow.value = true
}
// 关闭弹框
const handleClose = () => {
  dialogShow.value = false
}
// 更新数据
const handleUpdate = () => {
  emit("update")
}
</script>

<style scoped>
.project-box {
  display: flex;
  flex-direction: column;
  height: 100px;
  width: 300px;
  margin: 20px 0;
  justify-content: space-around;
  border-radius: 10px;
  padding: 10px;
  box-sizing: border-box;
  border: 1px solid #666;
  background-color: #eee;
  box-shadow: 10px 10px 5px rgba(0, 0, 0, .1);
}

.title {
  font-size: 17px;
  margin-left: 10px;
}

.title span:hover {
  color: #fc6d59;
  cursor: pointer;
}

.createTime {
  display: flex;
  justify-content: space-between;
}

.createTime span {
  margin: 0 10px;
}

.createTime .time span {
  font-size: 12px;
  color: #888;
  user-select: none;
}

.createTime .operation span {
  font-size: 14px;
  color: #666;
  cursor: pointer;
}

.createTime .operation span:hover {
  font-weight: bold;
}
</style>